package com.nokia.bpmn.provider.rest;

import com.fasterxml.jackson.core.type.TypeReference;
import com.seed.bpmn.engine.IBPMNHistoryService;
import com.seed.bpmn.engine.common.util.ResultData;
import com.seed.bpmn.engine.dto.ProcessInstanceDTO;
import com.seed.bpmn.engine.dto.TaskDTO;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * <p>流程流转记录相关服务接口</p>
 *
 * @author cscz89@126.com
 */
@Component("bpmnHistoryService")
public class BpmnHistoryServiceImpl extends BaseService implements IBPMNHistoryService {

    private static String BASE_HISTORY_URL = BASE_URL + "/histroy";

    @Override
    public ProcessInstanceDTO getProcessInstance(String processInstanceId) throws Exception {
        String url = BASE_HISTORY_URL + "/process-instance/{0}";
        LOGGER.info("通过流程实例ID获取历史级别流程实例:processInstanceId={}", processInstanceId);
        return get(new TypeReference<ResultData<ProcessInstanceDTO>>() {
        }, url, processInstanceId);
    }

    @Override
    public ProcessInstanceDTO getProcessInstanceByBusinessKey(String businessKey) throws Exception {
        return null;
    }

    @Override
    public ProcessInstanceDTO getFinishProcessInstance(String processInstanceId) {
        String url = BASE_HISTORY_URL + "/process-instance/finished/{0}";
        LOGGER.info("获取办结状态流程实例:processInstanceId={}", processInstanceId);
        return get(new TypeReference<ResultData<ProcessInstanceDTO>>() {
        }, url, processInstanceId);
    }

    @Override
    public TaskDTO getTask(String taskId) throws Exception {
        String url = BASE_HISTORY_URL + "/task/{0}";
        LOGGER.info("通过任务id查询历史级别任务:taskId={}", taskId);
        return get(new TypeReference<ResultData<TaskDTO>>() {
        }, url, taskId);
    }

    @Override
    public List<TaskDTO> findTaskByProcessInstanceId(String processInstanceId) throws Exception {
        String url = BASE_HISTORY_URL + "/task/process-instance-id/{0}";
        LOGGER.info("通过流程实例Id查询历史级别任务:processInstanceId={}", processInstanceId);
        return get(new TypeReference<ResultData<List<TaskDTO>>>() {
        }, url, processInstanceId);
    }
}
